// pages/lawhall/index.js
const app = getApp()
const { storeset, storetop, pay, storeApply } = require('../../utils/api')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        type: '',
        name: '',
        time: '',
        address: '',
        theContact: '',
        phone: '',
        idCard: '',
        theBusinessLicense: '',
        introduce: '',
        toptimes: '',
        topInfo: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(app.globalData.merchantsTypeList[options.index])
        this.setData({
            type: app.globalData.merchantsTypeList[options.index].text
        })
        storeset().then(res => {
            this.setData({
                toptimes: res.toptimes
            })
        })
        storetop().then(res => {
            console.log(res)
            this.setData({
                topInfo: res
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
    inputFn: function(data) {
        console.log(data.detail)
        let { dataName, value } = data.detail
        this.setData({
            [dataName]: value
        })
    },
    showToast: function(title) {
        wx.showToast({
            title,
            icon: 'none',
            duration: 1000
        })
    },
    publishFN: function(e) {
        console.log(e)
        let data = this.data
        console.log(data)
        if (!data.name.length) {
            this.showToast('请填写正确的商户名')
        } else if (!data.address.length) {
            this.showToast('请填写正确地址')
        } else if (!data.theContact.length) {
            this.showToast('请填写联系人')
        } else if (!data.theCphoneontact.length) {
            this.showToast('请填写联系电话')
        } else if (!data.idCard.length) {
            this.showToast('请填写身份证号')
        } else if (!data.theBusinessLicense.length) {
            this.showToast('请填写营业执照号')
        } else if (!data.introduce.length) {
            this.showToast('请填写商户介绍')
        }
        return false
        let obg = {
            area: '100101',
            openId: 'o6Xu15L9GLbilT5APcjbK7xzjMfs',
            memberId: '1',
            money: '100',
            orderdesc: 'test pay',
            orderId: new Date().getTime() + ''
        }
        pay(obg).then(info => {
            wx.requestPayment({
                timeStamp: info.timeStamp,
                nonceStr: info.nonceStr,
                package: `prepay_id=${info.prepayId}`,
                signType: 'MD5',
                paySign: info.paySign,
                success: function(res) {
                    wx.showToast({
                        title: '付款成功',
                        icon: 'success',
                        duration: 2000
                    })
                    setTimeout(() => {
                        wx.navigateTo({
                            url: '../../pages/order/index?type=1'
                        })
                    }, 2000)
                },
                fail: function(res) {
                    wx.showToast({
                        title: '取消付款成功',
                        icon: 'success',
                        duration: 2000
                    })
                }
            })
        })
        console.log(this.data)
    }
})
